package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.BlutoothCodeingBean;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BluetoothCodingActivity extends BaseAppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.pb)
    ProgressBar pb;
    private byte code;
    private List<BlutoothCodeingBean> blutoothCodeingBeans;
    private CommonAdapter<BlutoothCodeingBean> commonAdapter;

    @Override
    protected void getBundleExtras(Bundle extras) {
        code = extras.getByte("code");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_bluetooth_coding;
    }

    @Override
    protected void initViewsAndEvents() {
        blutoothCodeingBeans = new ArrayList<>();
        commonAdapter = new CommonAdapter<BlutoothCodeingBean>(this, blutoothCodeingBeans, R.layout.item_blutoothcode) {
            @Override
            public void convert(ViewHolder helper, int position, BlutoothCodeingBean item) {
                RelativeLayout rl = helper.getView(R.id.rl_code);
                TextView tv_code = helper.getView(R.id.tv_codeName);
                ImageView iv_code = helper.getView(R.id.iv_selector);
                tv_code.setText(item.getCodeName());
                if (item.isCheck()) {
                    iv_code.setImageResource(R.drawable.nav_icon_more_n);
                } else {
                    iv_code.setImageResource(R.drawable.nav_icon_more);
                }
                rl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean check = item.isCheck();
                        item.setCheck(!check);
                        notifyDataSetChanged();
                    }
                });

            }
        };
        lv.setAdapter(commonAdapter);

    }

    @Override
    protected void initData() {
        blutoothCodeingBeans.add(new BlutoothCodeingBean("HWA"));
        blutoothCodeingBeans.add(new BlutoothCodeingBean("LDAC"));
        blutoothCodeingBeans.add(new BlutoothCodeingBean("aptX"));
        blutoothCodeingBeans.add(new BlutoothCodeingBean("aptX-HD"));
        blutoothCodeingBeans.add(new BlutoothCodeingBean("AAC"));
        blutoothCodeingBeans.add(new BlutoothCodeingBean("aptX-LL"));
        blutoothCodeingBeans = getData(blutoothCodeingBeans);
        commonAdapter.refresh(blutoothCodeingBeans);
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case Constants.BLUETOOTH_STATE:
                BluetoothState state = (BluetoothState) eventCenter.getData();
                if (state == null) {
                    return;
                }
                switch (state.getState()) {
                    case Constants.DISCONNECTED:
                        ToastUtils.showLongToast(customApplication, R.string.bluetooth_disconnected);
                        finish();
                        break;
                }
                break;
        }
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @OnClick({R.id.iv_back, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_confirm:
                setBleCode();
                break;
        }
    }

    private void setBleCode() {
        byte code = 0;
        for (int i = 0; i < blutoothCodeingBeans.size(); i++) {
            if (blutoothCodeingBeans.get(i).isCheck()) {
                code = (byte) (code | (0x01 << i));

            }
        }
        EventBus.getDefault().postSticky(new EventCenter<>(Constants.GAIA_BLUTOOTH_CODE, new byte[]{code}));
        pb.setVisibility(View.VISIBLE);
        mHandler.postDelayed(new Runnable(){
            @Override
            public void run() {
                ToastUtils.showToast(BluetoothCodingActivity.this, R.string.Set_up_successfully);
                EventBus.getDefault().post(new EventCenter<>(Constants.BLE_CHANGE, preferenceUtil.getBleMac()));
                readyGoThenKill(MainActivity.class);
            }
        },2000);
    }
    public List<BlutoothCodeingBean> getData(List<BlutoothCodeingBean> data) {
        if (code == 0) {
            for (int i = 0; i < data.size(); i++) {
                data.get(i).setCheck(false);
            }
        } else {
            for (int i = 0; i < data.size(); i++) {
                int isCheck = (code >> i) & 0x01;
                if (isCheck == 1) {
                    data.get(i).setCheck(true);
                } else {
                    data.get(i).setCheck(false);
                }
            }
        }
        return data;
    }

}
